<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 * @version    v1.0.0 Beta @ 2008-10-16
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Controller 模块配置器
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Controller_Module
{

    /**
     * EGP_Controller_Module 名称
     *
     * @var string
     */
    private $_name = null;

    /**
     * 适配的主机名
     *
     * @var array
     */
    private $_hosts = array();

    /**
     * EGP_Controller_Module 路径配置
     *
     * @var array
     */
    private $_directory = array(
        'controller' => null,
        'model' => null,
        'view' => null,
    );

    /**
     * 默认名称配置
     *
     * @var array
     */
    private $_default = array(
        'controller' => 'index',
        'action' => 'index',
    );

    /**
     * 构造方法
     *
     * @param  string  $name  模块名称
     */
    public function __construct($name)
    {
        $this->setName($name);
    }

    /**
     * 获取 EGP_Controller_Front
     *
     * @return EGP_Controller_Front
     */
    public function getFrontController()
    {
        return EGP_Controller_Front::getInstance();
    }

    /**
     * 设置 EGP_Controller_Module 名称
     *
     * @param  string  $name
     * @return EGP_Controller_Module
     */
    public function setName($name)
    {
        $this->_name = strtolower($name);
        return $this;
    }

    /**
     * 获取 EGP_Controller_Module 名称
     *
     * @return string
     */
    public function getName()
    {
        return $this->_name;
    }

    /**
     * 配置 EGP_Controller_Module 选项
     *
     * @param  array  $options
     * @return EGP_Controller_Module
     * @throws EGP_Exception
     */
    public function setOptions(array $options)
    {
        foreach ($options as $type => $option) {
            if ($type == 'host') {
                !is_array($option) && $option = array($option);
                foreach ($option as $host)
                    $this->addHost($host);
            } elseif ($type == 'directory') {
                $this->setDirectory($option);
            } elseif ($type == 'default' && isset($option['controller'])) {
                $this->setDefaultController($option['controller']);
            } elseif ($type == 'default' && isset($option['action'])) {
                $this->setDefaultAction($option['action']);
            } else {
                throw new EGP_Exception("未知的选项设定 $type");
            }
        }
        return $this;
    }

    /**
     * 添加适配主机名
     *
     * @param  string  $httpHost
     * @return EGP_Controller_Module
     */
    public function addHost($httpHost)
    {
        $this->getFrontController()->getRouter()->addHost($httpHost, $this);
        $this->_hosts[] = $httpHost;
        return $this;
    }

    /**
     * 获取所有的适配主机名
     *
     * @return array
     */
    public function getHostList()
    {
        return $this->_hosts;
    }

    /**
     * 设置 EGP_Controller_Module 路径
     *
     * @param  array  $directory
     * @return EGP_Controller_Module
     */
    public function setDirectory(array $directory)
    {
        foreach ($directory as $key => $value) {
            if ($key == 'controller')
                $this->setControllerDirectory($value);
            elseif ($key == 'view')
                $this->setViewDirectory($value);
            elseif ($key == 'model')
                $this->setModelDirectory($value);
            else
                throw new EGP_Exception("不支持的模块属性设定 $key");
        }
        return $this;
    }

    /**
     * 设置 Controller 路径
     *
     * @param  string  $dir
     * @return EGP_Controller_Module
     */
    public function setControllerDirectory($dir)
    {
        $this->_directory['controller'] = (string) $dir;
        return $this;
    }

    /**
     * 设置 View 路径
     *
     * @param string $dir
     * @return EGP_Controller_Module
     */
    public function setViewDirectory($dir)
    {
        $this->_directory['view'] = (string) $dir;
        return $this;
    }

    /**
     * 设置 Model 路径
     *
     * @param string $dir
     * @return EGP_Controller_Module
     */
    public function setModelDirectory($dir)
    {
        $this->_directory['model'] = (string) $dir;
        return $this;
    }

    /**
     * 获取 Controller 路径
     *
     * @return string
     */
    public function getControllerDirectory()
    {
        return $this->_directory['controller'];
    }

    /**
     * 获取 View 路径
     *
     * @return string
     */
    public function getViewDirectory()
    {
        return $this->_directory['view'];
    }

    /**
     * 获取 View 路径
     *
     * @return string
     */
    public function getModelDirectory()
    {
        return $this->_directory['model'];
    }

    /**
     * 设置默认的 Controller
     *
     * @param  string  $controller
     * @return EGP_Controller_Module
     */
    public function setDefaultController($controller)
    {
        $this->_default['controller'] = (string) $controller;
        return $this;
    }

    /**
     * 获取默认的 Controller
     *
     * @return string
     */
    public function getDefaultController()
    {
        return $this->_default['controller'];
    }

    /**
     * 设置默认的 Action
     *
     * @param  string  $action
     * @return EGP_Controller_Module
     */
    public function setDefaultAction($action)
    {
        $this->_default['action'] = (string) $action;
        return $this;
    }

    /**
     * 获取默认的 Action
     *
     * @return string
     */
    public function getDefaultAction()
    {
        return $this->_default['action'];
    }

}